package com.library.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bean.Book;
import com.library.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Api(tags = "书籍管理")
@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bs;

    @ApiOperation(value = "获取书籍列表", notes = "根据条件分页获取书籍列表")
    @GetMapping("/getBooks")
    @ResponseBody
    public Object getBooks(
            @ApiParam(name = "book", value = "书籍查询条件") Book book,
            @ApiParam(name = "beginDate", value = "开始日期") Date beginDate,
            @ApiParam(name = "endDate", value = "结束日期") Date endDate,
            @ApiParam(name = "pageNum", value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNum,
            @ApiParam(name = "pageSize", value = "每页大小", defaultValue = "10") @RequestParam(required = false, defaultValue = "10") int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page = (Page<Book>) bs.getBooks(book, beginDate, endDate);
        for (Book book1 : page) {
            System.out.println(book1.getPubDate());
        }
        return page.toPageInfo();
    }

    @ApiOperation(value = "添加书籍", notes = "添加一本新书籍")
    @PostMapping("/addBook")
    public String addBook(
            @ApiParam(name = "book", value = "书籍信息", required = true) Book book,
            @ApiParam(name = "mf", value = "书籍封面图片", required = true) MultipartFile mf,
            Model model) {
        if (mf == null || mf.isEmpty() || !mf.getContentType().contains("image")) {
            model.addAttribute("msg", "必须选择封面，格式为图片");
            return "forward:/addBook.jsp";
        }

        try {
            String fileName = mf.getOriginalFilename();
            String newName = UUID.randomUUID() + fileName;
            String visitPath = "/22library2/bookCover/" + newName;
            String savePath = "c:/bookCover/" + newName;
            mf.transferTo(new File(savePath));
            book.setBookCover(visitPath);
            bs.addBook(book);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/book.jsp";
    }

    @ApiOperation(value = "删除书籍", notes = "根据书籍ID删除书籍")
    @RequestMapping("/deleteBook")
    @ResponseBody
    public String deleteBook(@ApiParam(name = "bookId", value = "书籍ID", required = true) int bookId) {
        Book book = bs.getBookById(bookId);

        int result = bs.deleteBook(bookId);
        if (result == 1) {
            String visitPath = book.getBookCover();
            String realPath = visitPath.replaceAll("/22library2/", "c:/");
            if (!realPath.equals("c:/bookCover/default.jpg")) {
                new File(realPath).delete();
            }
        }
        return result == 1 ? "success" : "fail";
    }

    @ApiOperation(value = "更新书籍页面", notes = "跳转到更新书籍页面")
    @RequestMapping("/update")
    public String updatePage(@ApiParam(name = "bookId", value = "书籍ID", required = true) int bookId, Model model) {
        Book book = bs.getBookById(bookId);
        model.addAttribute("book", book);
        return "forward:/WEB-INF/update.jsp";
    }

    @ApiOperation(value = "更新书籍", notes = "更新书籍信息")
    @RequestMapping("/updateBook")
    public String updateBook(
            @ApiParam(name = "book", value = "书籍信息", required = true) Book book,
            @ApiParam(name = "mf", value = "新的书籍封面图片") MultipartFile mf) {
        bs.updateBook(book);
        if (mf != null && !mf.isEmpty() && mf.getContentType().contains("image")) {
            // 查出修改后的书的信息，目的是获取到原来的封面路径
            book = bs.getBookById(book.getBookId());
            // 删除掉原来的封面的图片
            String visitPath = book.getBookCover();
            String realPath = visitPath.replaceAll("/22library2/", "c:/");
            if (!realPath.equals("c:/bookCover/default.jpg")) {
                new File(realPath).delete();
            }
            // 把用户提交的新封面上传到服务器
            String fileName = mf.getOriginalFilename();
            String newName = UUID.randomUUID() + fileName;
            String visit = "/22library2/bookCover/" + newName;
            String savePath = "c:/bookCover/" + newName;
            try {
                mf.transferTo(new File(savePath));
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 数据库修改书的新封面的访问路径
            bs.updateBookCover(book.getBookId(), visit);
        }
        return "redirect:/books";
    }
}